const Status = require('../../constant/status')
const Password = require('./password')

class User {
  constructor(properties) {
    Object.assign(this, properties)
    if (properties.password) {
      this.password = Password.fromHashed(properties.password)
    }
  }

  async verifyPassword(password) {
    return this.password.compare(password)
  }

  async canLogin() {
    return this.status === Status.ENABLED
  }

  async loginUser(password) {
    if (this.status !== Status.ENABLED) {
      return {
        success: false,
        message: `User is ${this.status?.toLowerCase()}.`
      }
    }

    const isPasswordValid = await this.verifyPassword(password)
    if (!isPasswordValid) {
      return { success: false, message: 'Invalid credentials.' }
    }

    return { success: true, message: 'Login successful' }
  }
}

module.exports = User
